package com.example.exam.servlet;

import com.example.exam.dao.ExamDAO;
import com.example.exam.dao.ScoreDAO;
import com.example.exam.entity.Exam;
import com.example.exam.entity.Score;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 仪表盘Servlet
 */
@WebServlet("/dashboard")
public class DashboardServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private ExamDAO examDAO = new ExamDAO();
    private ScoreDAO scoreDAO = new ScoreDAO();
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // 检查用户是否已登录
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("student") == null) {
            response.sendRedirect(request.getContextPath() + "/login");
            return;
        }
        
        // 获取学生ID
        String studentId = ((com.example.exam.entity.Student) session.getAttribute("student")).getStudentId();
        
        // 获取即将进行的考试
        List<Exam> upcomingExams = examDAO.getAvailableExams(studentId);
        request.setAttribute("upcomingExams", upcomingExams);
        
        // 获取最近的成绩
        List<Score> recentScores = scoreDAO.getScoresByStudentId(studentId);
        request.setAttribute("recentScores", recentScores);
        
        // 转发到仪表盘页面
        request.getRequestDispatcher("/WEB-INF/views/dashboard.html").forward(request, response);
    }
}    